<template>
	<view class="">
		<!-- 会员授权 -->
		<u-popup :round="10" :show="show" @close="close" @open="open">
			<view class="content">
				<view class="flex-column">
					<view class="flex-column align-center justify-center">
						<button class="box-img" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
							<u-avatar :src="userInfo.avatar" size="60"></u-avatar>
						</button>
						<view class="" style="margin-top: 10rpx;">
							上传头像
						</view>
					</view>

					<view class="" style="margin-top: 60rpx;">
						<u-input v-model="userInfo.nickname" type="nickname" class="weui-input" placeholder="请输入昵称"
							@blur="onNickname" />
					</view>
				</view>
				<u-button :loading="loading" shape="circle" :color="$u.color.linearColor" open-type="getUserInfo" type="primary" text="确定登录授权" @click="submit"></u-button>
			</view>
		</u-popup>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		wxLogin
	} from '@/common/api.js'
	import {
		baseUrl
	} from '@/common/config.js'
	import {
		mapState
	} from "vuex";
	export default {
		props: {
			// show: {
			// 	type: Boolean,
			// 	default: false
			// },
		},
		data() {
			return {
				loading:false,
				show: false,
				userInfo: {
					'nickname': "",
					'avatar': ""
				}
			}
		},
		computed: {
			...mapState(["isLogin"]),
		},
		methods: {
			onNickname(e) {
				console.log(e, '---onNickname');
				this.userInfo.nickname = e
			},
			async onChooseAvatar(e) {
				uni.showLoading({
					title: '正在上传...'
				})
				console.log(e, '---onChooseAvatar');
				const result = await this.uploadFilePromise(e.detail.avatarUrl);
				console.log(result, '---result');
				this.userInfo.avatar = result;
				uni.hideLoading();
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseUrl + '/api/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						// header: {
						// 	token: this.token
						// },
						success: (res) => {
							let data = JSON.parse(res.data);
							setTimeout(() => {
								console.log('res', data.data.url);
								resolve(data.data.fullurl)
							}, 1000)
						}
					});
				})
			},
			open() {},
			close() {},
			submit(e) {
				let vm = this;
			
				uni.showLoading({
					title: '登录中....'
				})
				if (this.userInfo.avatar == '') {
					this.$u.toast('请上传头像');
					return false;
				};
				if (this.userInfo.nickname == '') {
					this.$u.toast('请输入昵称');
					return false;
				};
				this.loading = true
				uni.login({
					provider: 'weixin',
					success: function(res) {
						console.log(vm.userInfo);
						let params = {
							'code': res.code,
							'avatar': vm.userInfo.avatar,
							'nickname': vm.userInfo.nickname,
						}
						wxLogin(params).then(res => {
							uni.hideLoading();
							vm.$refs.uToast.show({
								type: 'success',
								message: '授权成功',
								complete() {
									vm.loading = false
									uni.hideLoading()
									vm.$store.commit("setUserInfo", res
										.userinfo);
									vm.show = false;
									let pages = getCurrentPages();
									uni.reLaunch({
										url: '/' + pages[0].route
									})
								}
							})
						})
					}
				})
				console.log('成功');

			}
		}
	}
</script>

<style>
	.content {
		height: 500rpx;
		padding: 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
	}

	.box-img {
		border: none;
		background-color: transparent;
	}

	.box-img::after {
		content: ' ';
		width: 0%;
		height: 0%;
		border: none;
	}
</style>